<?php
/**
 * Phone Number Rule
 * Validates phone numbers (as of now only US phone numbers)
 *
 * @package     Visi
 * @subpackage  Rules
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2011 Luke Visinoni <luke.visinoni@gmail.com>
 * @version     $Id$
 */
class Visi_Rule_Phone extends Q_Rule_Abstract {

    /**
     * The parameters that were passed to this rule
     */
    protected $_params = array(
        'requireAreaCode' => true,
        'allowOnePrefix' => true,
    );
    
    public function isValid($value) {
    
        $number = $this->_getDigits($value);
        $length = strlen($number);
        $area = '';
        $one = '';
        if ($this->getParam('requireAreaCode')) {
            $area = '[0-9]{3}';
        }
        if ($this->getParam('allowOnePrefix')) {
            $one = '1?';
        }
        $pattern = "/^{$one}{$area}[0-9]{7}$/";
        return $this->_validatePattern($pattern, $value);
    
    }
    
    protected function _getDigits($number) {
    
        return preg_replace('/[^a-z]+/i', '', $number);
    
    }

}
